package com.longshine.tools.util;

import java.io.File;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;



public class CommonUtil {

	public static Map<String,String> str2Map(String src){
		String[] styles = src.split(";");
		Map<String,String> m = new HashMap<String,String>();
		for(String style:styles){
			String kv[] = style.split(":");
			m.put(kv[0], kv[1]);
		}
		return m;
	}
	public static String map2Str(Map<String,String> map){
		String rt = "";
		for(Object o:map.entrySet()){
			String k = ((Map.Entry)o).getKey().toString();
			String v  = ((Map.Entry)o).getValue().toString();
			rt  = rt + k+":"+v+";";
		}
		rt = rt.substring(0, rt.length()-1);
		return rt;
	}

	
	
	public static String getSvgUrl(String fileName){
		File f = new File(fileName);
		try {
			return f.toURL().toString();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public static void delay(long ms){
		if(ms <=0){
			return;
		}
		try {
			Thread.sleep(ms);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public static String formatState(int state){
		return String.format("%02d", state);
	}

	public static int[] list2IntArr(List<Integer> l){
		if(l==null) return null;
		int[] ret = new int[l.size()];
		for(int i=0;i<l.size();i++){
			ret[i] = l.get(i);
		}
		return ret;
	}
	/**
	 * 取得系统类型与编号的组合值
	 * @param sysType
	 * @param downNo
	 * @return
	 */
	public static int getHashValue(int sysType,int downNo){
		return (sysType<<4)+downNo;
	}
	public static int getSysTypeByHashValue(int value){
		return value>>4;
	}
	public static int getDownNoTypeByHashValue(int value){
		return value%16;
	}
	
	public static String getTodayValue(){
		Date d = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		String s = sf.format(d);
		return s;
	}
	public static String getCurrentTime(){
		Date d = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
		String s = sf.format(d);
		return s;
	}
	
	public static Date getCurrentDate(){
		return new Date();
	}
	
	public static String formatTime(Date date){
		if(date==null) return null;
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = sf.format(date);
		return s;
	}
	
	
	
	public static void main(String[] args){
//		System.out.println(toHex("20"));
//		Date d = new Date();
//		Date s = new Date(0,0,0,8,0,0);
//		System.out.println(d.getHours());
		System.out.println(toHexStr(254));
	}
	public static String getCurrentPath(){
		File f = new File("./");
		return DirUtil.formatURL(DirUtil.getParentPath(f.getAbsolutePath()));
	}

	public static int binary2Decimal(String s){
		char[] cs = s.toCharArray();
		int ret = 0;
		for(int i=0;i<cs.length;i++){
			ret += Math.pow(2, cs.length-i-1)*(cs[i]=='1'?1:0); 
		}
		return ret;
	}
	/**
	 * 将16进制字符串转为10进制
	 * @param num
	 * @return
	 */
	public static int toHex(String num){
		int ret = 0;
		num = num.toLowerCase();
		char[] chars = num.toCharArray();
		for(int i =0;i<chars.length;i++){
			char c = chars[i];
			ret = ret<<4;
			ret += (c>='a'?(10+c-'a'):(c-'0'));
		}
		return ret;
	}
	/**
	 * 十进制转 十六进制
	 * @param num 0-255
	 * @return
	 */
	public static String toHexStr(int num){
		int t = num;
		String ret = "";
		int devidedNum = t / 16;
		int leftNum = t % 16;
		ret += toHexChar(devidedNum);
		ret += toHexChar(leftNum);
		return ret;
	}
	/**
	 * 将少于16的数转化为十六进制字符
	 * @param num <16
	 * @return
	 */
	private static char toHexChar(int num){
		char ret = '0';
		if(num<10){
			ret = (char)num;
		}
		else{
			ret =(char)('a'+(num-10));
		}
		return ret;
	}
	
	public static Integer[] intArr2IntegerArr(int intArr[]){
		Integer[] ret = new Integer[intArr.length];
		for(int i =0;i<ret.length;i++){
			ret[i] = intArr[i];
		}
		return ret;
	}
	
	public static LinkedList arr2List(int intarr[]){
		LinkedList l = new LinkedList();
		for(int i:intarr){
			l.add(i);
		}
		return l;
	}
	
	public static Object[] list2Arr(List l){
		Object[] os = new Object[l.size()];
		return l.toArray(os);
	}
	
	public static String getUrl(File f) {
//		String s = "";
//		if (f.exists()) {
//			if (f.isDirectory()) {
//				s = null;
//			} else {
//				try {
//					s = f.getCanonicalPath();
//					if (s.startsWith("/")) {
//						s = "file:" + s;
//					} else {
//						s = "file:/" + s;
//					}
//				} catch (IOException ex) {
//				}
//			}
//		}
		try {
			return f.toURL().toString();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 
	 * 方法说明：当前日期为准，获取某天日期字符串
	 *
	 * Author：        DiaolinH                
	 * Create Date：   Mar 28, 2013 2:09:24 PM
	 * History:  Mar 28, 2013 2:09:24 PM   DiaolinH   Created.
	 *
	 * @param tempNum 往前往后 天数
	 * @return
	 *
	 */
	public static String getOneDayStr(int tempNum){
		
		 Date date=new Date();
		 Calendar calendar = new GregorianCalendar();
		 calendar.setTime(date);
		 calendar.add(calendar.DATE,tempNum);
		 date=calendar.getTime(); 
		 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		 String dateString = formatter.format(date);
		 
		 return dateString;
	}
}
